// miniprogram/pages/NX_register/register.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: [],
        step: 0,
        titleList: ['欢迎来到Nexeam协同小程序', '您希望被其他项目成员怎样称呼？', ''],
        name: '',
        randomColor: [],
        isLoading:false
    },
    onchange(e) {
        this.data.name = e.detail.value;
    },
    submit() {
        if(this.data.name.length==0){
            wx.showToast({
                title: '请输入昵称',
                icon:'none'
            })
            return;
        }
        wx.showLoading({
            title: '请稍后',
        })
        this.setData({
            isLoading:true,
        })
        wx.cloud.callFunction({
            name: "register",
            data: {
                openid: getApp().globalData.openid,
                userName: this.data.name
            }
        }).then(res => {
            wx.cloud.callFunction({
                name: 'login'
            }).then(res => {
                wx.hideLoading();
                getApp().globalData.userInfo = res.result.userInfo
                wx.redirectTo({
                    url: '../NX_selfProject/selfProject',
                })
            }).catch(err => {
                wx.showToast({
                    title: '发生错误',
                })
                this.setData({
                    isLoading: false,
                })
                console.log(err)
            });
        })
    },
    /**
     * 
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        wx.setNavigationBarTitle({
            title: '',
        })
        this.nextStep();
        let randomColor = [];
        const randomFF = (num) => {
            return (Math.ceil(num * Math.random()))
        }
        for (let i = 0; i < 4; i++) {
            randomColor.push(`rgba(${randomFF(100)},${randomFF(155)+100},${randomFF(100)},${Math.random()*0.5+0.3})`)
        }
        this.setData({
            randomColor
        })
    },
    nextStep() {
        if (this.data.step < this.data.titleList.length) {
            this.data.title[this.data.step] = this.data.titleList[this.data.step]
            this.setData({
                title: this.data.title,
                step: this.data.step + 1
            })
            setTimeout(() => {
                this.nextStep()
            }, 1500)
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        this.setData({
            step: 0,
            title: []
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

})